using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_TraLoiDanhSach : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            loadDropDownList();
            loadData();
        }
    }
    private void loadDropDownList()
    {

        tbTraLoiCollection tlCollection = tbTraLoiBUS.GetAll();
        ddl_matraloi.DataTextField = tbTraLoiColumns.MaTL.ToString();
        ddl_matraloi.DataSource = tlCollection;
        ddl_matraloi.DataBind();
        ddl_matraloi.Items.Insert(0, new ListItem("All", "0"));
        ddl_ngaytraloi.DataTextField = tbTraLoiColumns.NgayTraLoi.ToString();
        ddl_ngaytraloi.DataSource = tlCollection;
        ddl_ngaytraloi.DataBind();
        ddl_ngaytraloi.Items.Insert(0, new ListItem("All", "0"));
        
        tbKhachHangCollection khCollection = tbKhachHangBUS.GetAll();
        ddl_khachhang.DataTextField  = tbKhachHangColumns.HoTen.ToString();
        ddl_khachhang.DataValueField = tbKhachHangColumns.MaKH.ToString();
        ddl_khachhang.DataSource = khCollection;
        ddl_khachhang.DataBind();
        ddl_khachhang.Items.Insert(0, new ListItem("All", "0"));

        
    }
    private void loadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();
        if (ddl_matraloi.Text != "0")
        {
            c.add(Condition.AND, tbTraLoiColumns.MaTL, Condition.LIKE, ddl_matraloi.SelectedValue);
        }
        if (ddl_ngaytraloi.SelectedValue != "0")
        {
            c.add(Condition.AND, tbTraLoiColumns.NgayTraLoi, Condition.EQUAL, ddl_ngaytraloi.SelectedValue);
        }
        if (ddl_khachhang.SelectedValue != "0")
        {
            c.add(Condition.AND, tbTraLoiColumns.KhachHang, Condition.EQUAL, ddl_khachhang.SelectedValue);
        }
        int total = 0;
        tbTraLoiCollection tlCollection =
        tbTraLoiBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
        tbTraLoiColumns.MaTL.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = tlCollection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
        {
            Pager.Visible = false;
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            tbTraLoi traloi = (tbTraLoi)e.Item.DataItem;
            Literal lit_MaTL = (Literal)e.Item.FindControl("lit_MaTL");
            Literal lit_BinhLuan = (Literal)e.Item.FindControl("lit_BinhLuan");
            Literal lit_KhachHang = (Literal)e.Item.FindControl("lit_KhachHang");
            Literal lit_TraLoi = (Literal)e.Item.FindControl("lit_TraLoi");
            Literal lit_NgayTraLoi = (Literal)e.Item.FindControl("lit_NgayTraLoi");
            tbBinhLuan binhluan = tbBinhLuanBUS.GetByID(traloi.BinhLuan);
            lit_BinhLuan.Text = binhluan.NoiDung;

            tbKhachHang khachhang = tbKhachHangBUS.GetByID(traloi.KhachHang);
            lit_KhachHang.Text = khachhang.HoTen;
            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/Admin/TraLoiUpdate.aspx?id=" + traloi.MaTL.ToString();

        }
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        loadData();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        loadData();
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int iD = int.Parse(hf_id.Value);
                tbTraLoi traloi = tbTraLoiBUS.GetByID(iD);
                tbTraLoiBUS.Delete(iD);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
}